package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.common.Edge;
import ru.amse.tsyganov.jumleditor.view.common.EdgeView;

/*package*/ class DeleteEdgeCommand<T extends Edge> extends Command {
	
	private final EdgeView<T> edgeView;
	
	private final Edge edge;
	
	public DeleteEdgeCommand(
			EdgeView<T> edgeView) {
		
		if (edgeView == null) {
			throw new IllegalArgumentException();
		}
		
		this.edge = edgeView.getModel();
		this.edgeView = edgeView;
	}
	
	@Override
	public void execute() {
		
		edgeView.getModelViewMap().getEdgesMap().
			remove(edge);
		
		edgeView.getSelectedItems().
			selectedTransitionsRemove(edgeView);
		
		Edge t = edgeView.getModel();
		t.getSource().removeOutgoingTransition(t);
		t.getTarget().removeIncomingTransition(t);	
	}

	@Override
	public void unexecute() {
		
		edgeView.getModelViewMap().getEdgesMap().
			put(edge, edgeView);
		
		Edge t = edgeView.getModel();
		t.getSource().addOutgoingTransition(t);
		t.getTarget().addIncomingTransition(t);
	}
}
